package com.zqj.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.annotation.PreventDuplicateSubmit;
import com.zqj.project.common.entity.R;
import com.zqj.project.common.request.PageParams;
import com.zqj.project.converter.UserConverter;
import com.zqj.project.dto.UserDTO;
import com.zqj.project.service.UserService;
import com.zqj.project.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description: UserController
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 19:38
 * @Version: 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user")
@Tag(name = "User API", description = "用户管理接口，支持增删改查以及分页功能")
@RequiredArgsConstructor
public class UserController {

    // 自动生成依赖注入的构造函数
    private final UserService userService;

    // 分页查询用户列表
    @Operation(summary = "分页查询用户列表", description = "根据分页参数查询用户信息")
    @PostMapping("/page")
    public R<IPage<UserVO>> queryUserPage(@RequestBody @Validated @NotNull @Parameter(description = "分页参数及筛选条件") PageParams<UserDTO> pageParams) {
        UserDTO userDTO = pageParams.getModel();
        return R.success(UserConverter.toUserVoPage(this.userService.page(UserConverter.toUserModel(userDTO), new Page<>(pageParams.getCurrent(), pageParams.getSize()))));
    }

    // 查询用户详情
    @Operation(summary = "查询用户详情", description = "根据用户ID查询具体用户的详细信息")
    @GetMapping("/info/{id}")
    public R<UserVO> info(@PathVariable @Parameter(description = "用户ID", required = true) Long id) {
        return R.success(UserConverter.toUserVo(this.userService.info(id)));
    }

    // 创建用户
    @Operation(summary = "创建用户", description = "创建一条新的用户记录")
    @PostMapping("/add")
    @PreventDuplicateSubmit
    public R<String> add(@RequestBody @Validated @Parameter(description = "要创建的用户信息") UserDTO userDto) {
        return Boolean.TRUE.equals(this.userService.add(UserConverter.toUserModel(userDto))) ? R.success("用户创建成功") : R.fail("用户创建失败");
    }

    // 编辑用户信息
    @Operation(summary = "编辑用户信息", description = "根据用户ID编辑用户信息")
    @PutMapping("/edit")
    @PreventDuplicateSubmit(expireTime = 5)
    public R<String> edit(@RequestBody @Validated @Parameter(description = "要编辑的用户信息，需包含ID") UserDTO userDto) {
        if (Objects.isNull(userDto.getId())) {
            return R.fail("用户ID不能为空");
        }
        return Boolean.TRUE.equals(this.userService.edit(UserConverter.toUserModel(userDto))) ? R.success("用户信息编辑成功") : R.fail("用户信息编辑失败");
    }

    // 删除用户
    @Operation(summary = "删除用户", description = "根据用户ID删除用户信息")
    @DeleteMapping("/delete/{id}")
    @PreventDuplicateSubmit(expireTime = 50)
    public R<String> delete(@PathVariable @Parameter(description = "用户ID", required = true) Long id) {
        return Boolean.TRUE.equals(this.userService.delete(id)) ? R.success("用户删除成功") : R.fail("用户删除失败");
    }


    // 用户登录
    @Operation(summary = "用户登录", description = "用户登录的验证")
    @PostMapping("/login")
    @PreventDuplicateSubmit
    public R<String> login(@RequestBody @Validated @Parameter(description = "要新增的用户信息") UserDTO userDto) {
        return Boolean.TRUE.equals(this.userService.login(UserConverter.toUserModel(userDto))) ? R.success("登录成功") : R.fail("用户名或密码错误");
    }

    // 用户注册
    @Operation(summary = "用户注册", description = "用户注册")
    @PostMapping("/register")
    @PreventDuplicateSubmit
    public R<String> register(@RequestBody @Validated @Parameter(description = "要注册的用户信息") UserDTO userDto) {
        if (!Objects.equals(userDto.getPassword(), userDto.getRepeatPassword())) {
            return R.fail("两次输入的密码不一致");
        }
        return Boolean.TRUE.equals(this.userService.register(UserConverter.toUserModel(userDto))) ? R.success("用户注册成功") : R.fail("用户注册失败");
    }

}